package com.zang.web.interceptor;

import com.zang.config.PublicView;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * 检查注解
 * @author Zhang Qiang
 * @date 2019/10/23 15:22
 */
public class ViewInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof Method){
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            if (method.isAnnotationPresent(PublicView.class)){
                PublicView publicView = method.getAnnotation(PublicView.class);
                if (publicView.required()){
                    return false;
                } else {
                    return true;
                }
            }
        }
        return true;
    }
}
